package com.one.common.view.dialog;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.one.common.R;
import com.one.common.utils.PermissionsUtils;
import com.one.common.utils.StringUtils;
import com.one.common.view.multitytype.MultiTypeAdapter;
import com.one.common.view.multitytype.adapter.BaseItem;
import com.one.common.view.multitytype.adapter.BaseItemBinder;

import java.util.ArrayList;

/**
 * creator: zhulunjun
 * time:    2017/9/13
 * describe: 对弹窗的封装操作
 */

public class AutoDialogHelper {

//    public static AutoDialog autoDialog;


    /***
     *  包含 标题显示与否 的 弹窗
     * @param context
     * @param title
     * @param content
     * @param leftBtnText
     * @param rightBtnText
     * @param leftListener
     * @param rightListener
     * @return
     */
    public static AutoDialog showContent(Context context, String title, String content, String leftBtnText, String rightBtnText,
                                         final OnConfirmListener leftListener, final OnConfirmListener rightListener) {
        AutoDialog autoDialog = new AutoDialog(context);
        autoDialog.setContentAndGravity(content, Gravity.LEFT);
        autoDialog.setTitle(title);

        if (StringUtils.isNotBlank(leftBtnText)) {
            autoDialog.setLeftText(leftBtnText);
        }
        if (StringUtils.isNotBlank(rightBtnText)) {
            autoDialog.setRightText(rightBtnText);
        }
        autoDialog.setOnBothConfirmListener(new AutoDialog.OnBothConfirmListener() {
            @Override
            public void onLeftClick() {
                if (leftListener != null) {
                    leftListener.onClick();
                }
                if (autoDialog != null) autoDialog.toggleShow();
            }

            @Override
            public void onRightClick() {
                if (rightListener != null) {
                    rightListener.onClick();
                }
                if (autoDialog != null) autoDialog.toggleShow();
            }
        });
        autoDialog.toggleShow();
        return autoDialog;
    }




    /**
     * 只需要显示内容的dialog，带确认和取消按钮
     *
     * @param context       上下文
     * @param content       内容
     * @param leftBtnText   左边按钮文本
     * @param rightBtnText  右边按钮文本
     * @param rightListener 确认监听
     * @param leftListener  取消监听
     */
    public static AutoDialog showContent(Context context, String content, String leftBtnText, String rightBtnText,
                                         final OnConfirmListener leftListener, final OnConfirmListener
                                                 rightListener) {
//        if (autoDialog != null) {
//            autoDialog.dismiss();
//            autoDialog = null;
//        }
//        if(autoDialog == null) {
        AutoDialog autoDialog = new AutoDialog(context);
//        }
        autoDialog.setContent(content);
        if (StringUtils.isNotBlank(leftBtnText)) {
            autoDialog.setLeftText(leftBtnText);
        }
        if (StringUtils.isNotBlank(rightBtnText)) {
            autoDialog.setRightText(rightBtnText);
        }
        autoDialog.setOnBothConfirmListener(new AutoDialog.OnBothConfirmListener() {
            @Override
            public void onLeftClick() {
                if (leftListener != null) {
                    leftListener.onClick();
                }
                if (autoDialog != null) autoDialog.toggleShow();
            }

            @Override
            public void onRightClick() {
                if (rightListener != null) {
                    rightListener.onClick();
                }
                if (autoDialog != null) autoDialog.toggleShow();
            }
        });
//        if(!autoDialog.isShowing()) {
//            autoDialog.show();
//        }else {
////            Toaster.showLongToast("显示中...");
//        }
        autoDialog.toggleShow();
//        autoDialog.setOnDismissListener(dialog -> {
//            if(autoDialog!=null) {
//                autoDialog.dismiss();
//                autoDialog = null;
//            }
//        });
        return autoDialog;
    }

    /**
     * 只需要显示内容的dialog，带确认和取消按钮
     *
     * @param context         上下文
     * @param content         内容
     * @param confirmListener 确认监听
     */
    public static AutoDialog showContent(Context context, String content, final OnConfirmListener confirmListener) {
        return showContent(context, content, null, null, confirmListener);
    }

    /**
     * 只需要显示内容的dialog，带确认和取消按钮
     *
     * @param context         上下文
     * @param content         内容
     * @param confirmListener 确认监听
     * @param leftBtnText     左边按钮文本
     * @param rightBtnText    右边按钮文本
     */
    public static AutoDialog showContent(Context context, String content, String leftBtnText, String rightBtnText,
                                         final OnConfirmListener confirmListener) {
        return showContent(context, content, leftBtnText, rightBtnText, null, confirmListener);
    }

    /**
     * 只需要显示内容的dialog
     * 只显示一个按钮，我知道了
     */
    public static AutoDialog showContentOneBtn(Context context, String content, String btnText, final
    OnConfirmListener onConfirmListener) {
        AutoDialog autoDialog = getContentOneBtn(context, content, btnText, onConfirmListener);
        autoDialog.toggleShow();
        return autoDialog;
    }

    /**
     * 获取一个单个按钮的弹窗
     */
    public static AutoDialog getContentOneBtn(Context context, String content, String btnText, final
    OnConfirmListener onConfirmListener) {
        AutoDialog autoDialog = new AutoDialog(context);
//        }
        autoDialog.setContent(content);
        if (StringUtils.isNotBlank(btnText)) {
            autoDialog.setSingleText(btnText);
        }
        autoDialog.setOnSingleConfirmListener(() -> {
            if (onConfirmListener != null) {
                onConfirmListener.onClick();
            }
            if (autoDialog != null)
                autoDialog.toggleShow();
        });
        return autoDialog;
    }

    public static AutoDialog showContentOneBtn(Context context, String content) {
        return showContentOneBtn(context, content, null, null);
    }

    public static AutoDialog showContentOneBtn(Context context, String content, String btnText) {
        return showContentOneBtn(context, content, btnText, null);
    }


    public interface OnConfirmListener {
        void onClick();
    }


    /**
     * 放弃承接的弹窗
     *
     * @param context         上下文
     * @param confirmListener 监听
     * @return 弹窗
     */
    public static AutoDialog showGiveUpOrderDialog(Context context, OnConfirmListener confirmListener) {
        String content = "确认要放弃当前的运输订单吗？";
        return showContent(context, content, confirmListener);
    }

    /**
     * 设置定位权限的弹窗
     *
     * @return 弹窗
     */
    public static AutoDialog showSettingLocationPermission(final Context context) {
//        if (autoDialog == null) {
        AutoDialog autoDialog = new AutoDialog(context);
//        }
        String versionName = "";
        versionName = "司机版";

        autoDialog.setTitle("无法获取您当前位置");
        autoDialog.setContent("请打开手机“系统设置”->“应用程序信息”->“权限管理”打开定位服务，允许“560" + versionName + "”使用您的位置。");
        autoDialog.setRightText("设置");
        autoDialog.setLeftText("暂不");
        autoDialog.setOnBothConfirmListener(new AutoDialog.OnBothConfirmListener() {
            @Override
            public void onLeftClick() {
                if (autoDialog != null) autoDialog.dismiss();
            }

            @Override
            public void onRightClick() {
                PermissionsUtils.goToSyetemSetting(context);
            }
        });
        return autoDialog;
    }


    /**
     * 查看底部类别弹窗
     */
    public static <T extends BaseItem> BottomSheetDialog showListDialog(Context context, String title, Class<T> tClass,
                                                                        BaseItemBinder binder, ArrayList<T> listData) {
        BottomSheetDialog dialog = new BottomSheetDialog(context);
        View view = LayoutInflater.from(context).inflate(R.layout.dialog_bottom_sheet_list, null);
        ImageView ivCancel = view.findViewById(R.id.iv_cancel);
        ivCancel.setOnClickListener(v -> dialog.dismiss());
        TextView tvTitle = view.findViewById(R.id.tv_title);
        if (StringUtils.isNotBlank(title)) {
            tvTitle.setText(title);
        }
        RecyclerView rcList = view.findViewById(R.id.rc_list);
        rcList.setLayoutManager(new LinearLayoutManager(context));
        MultiTypeAdapter adapter = new MultiTypeAdapter();
        adapter.register(tClass, binder);
        adapter.setItems(listData);
        rcList.setAdapter(adapter);

        dialog.setContentView(view);

        dialog.show();
        return dialog;
    }


}
